<?php

class Sanity_AdminController extends Ac_AdminController
{
    public function dashboardAction()
    {
        $this->view->num_active_errors = Model_Sanity::all()->count();
    }

    public function menuAction()
    {
        $this->view->index = $this->_getParam('index');
    }

    public function indexAction() 
    {
        $this->listAction();
    }
    
    public function listAction() 
    {
        $per_page = 20;
        $listing_object = array('status'=>1);
        $listing_error = array();
        $page      = !empty($_GET['page']) ? $_GET['page'] : 1;
        $this->view->per_page = $per_page;
        $this->view->listing_number = Model_Sanity::all(array('path' => array('$exists' => 0)))->count();
        $this->view->page = $page;
        $skip_page = 0;
        
        if ($page>1) {
            $skip_page = ($_GET['page']-1) * $per_page;
        }
        //errors
		if(!empty($_GET['errorz'])){
			$listing_error = array('errors.' . $_GET['errorz'] => array('$exists'=>true));
			print_r($listing_error);
			//exit();
		}	
        
        //object
        if(!empty($_GET['object']) && $_GET['object']!=0){
			switch($_GET['object']){
				case 1:
					$listing_object = array('_type'=>'Model_Listing_Boat', 'status'=>1);
					break;
				case 2:
					$listing_object = array('_type'=>'Model_Listing_Car', 'status'=>1);
					break;
				case 3:
					$listing_object = array('_type'=>'Model_Listing_Commercial', 'status'=>1);
					break;
				case 4:
					$listing_object = array('_type'=>'Model_Listing_Maid', 'status'=>1);
					break;
				case 5:
					$listing_object = array('_type'=>'Model_Listing_Motorcycle', 'status'=>1);
					break;
				case 6:
					$listing_object = array('_type'=>'Model_Listing_Residential', 'status'=>1);
					break;
			}
			//errors
			if(!empty($_GET['errorz'])){
				//$listing_object = array_merge($listing_object, array('errors.' . $_GET['errorz'] => array('$exists'=>true)));
				//print_r($listing_error);
				//exit();
			}	
			$by_type = Model_Listing::all($listing_object);
			$ids = array();
			foreach($by_type as $item){
				$ids[]=$item->_id;	
			}
		}		
		
			
		echo Model_Listing::all(array('_type'=>'Model_Listing_Maid', 'status'=>1))->count()."<<<----------------------";
		if(empty($_GET['object'])){
			$this->view->list = Model_Sanity::all($listing_error)->skip($skip_page)->limit($per_page)->sort(array('error_count'=>-1));
		}else{
			if(!empty($_GET['errorz'])){
				$this->view->list = Model_Sanity::all(array_merge(array('ls'=>array('$in'=>$ids)), array('errors.' . $_GET['errorz'] => array('$exists'=>true))))->skip($skip_page)->limit($per_page)->sort(array('error_count'=>-1));
			}else{
				$this->view->list = Model_Sanity::all(array('ls'=>array('$in'=>$ids)))->skip($skip_page)->limit($per_page)->sort(array('error_count'=>-1));
			}
				
		}
        	//$this->view->list = Model_Sanity::all(array('ls'=>array('$in'=>$ids), 'errors.district'=>array('$exists'=>true)))->skip($skip_page)->limit($per_page)->sort(array('error_count'=>-1));
        //$this->view->list = Model_Sanity::all(array('ls'=>array('$in'=>$ids)))->skip($skip_page)->limit($per_page)->sort(array('error_count'=>-1));
        $this->view->listing_number = $this->view->list->count();
		
		$this->view->error_list = Model_Sanity::getErrorKey();
        $this->render('list');  
    }
  
    public function displayAction()
    {
        //manual photo fix
        if(!empty($_GET['fix']) && $_GET['fix']=="photo"){
        	$photos = array();
        	$flag = false;
	        $ad = Model_Listing::one(array('_id' => new MongoId($_GET['id'])));
	        
	        if (!empty($ad->ph)) {
	            foreach ($ad->ph as $photo) {
	            	if (file_exists(APPLICATION_PATH . '../public/images/photo/original/' . $photo)) {
	                    $photos[] = $photo;
	                }else{
						$flag = true;
					}
	            }
	        }
	        
	        if($flag){
				$ad->ph = array_values($photos);
				$ad->save();
				//echo "<pre>";var_dump($photos);echo "</pre>";
			}
        }
        
        $sanity = Model_Sanity::find($_GET['id']);
        
        $this->view->sanity = $sanity;
        $this->render('display');
    }
    
    public function makeAction(){
		
		$this->render('test');
	}
    
}
